package com.example.appstudy2405;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.example.appstudy2405.dto.ViewHoldRCDto;

public class BitCatP23RecyViewActivity extends AppCompatActivity {

    private RecyclerView listRv;

    private String[] titles = {"桌子", "SUV", "蛋糕", "苹果", "毛线衣", "猕猴桃", "茶杯"};

    private String[] introduces = {
            "现代简约风格办公桌",
            "家用SUV城市越野车",
            "奶油水果夹心蛋糕",
            "新鲜有机红富士苹果",
            "手工编织羊毛毛线衣",
            "猕猴桃猕猴桃猕猴桃猕猴桃",
            "骨瓷手绘艺术茶杯"
    };

    private int[] icons = {R.drawable.desk, R.drawable.suv, R.drawable.cake, R.drawable.apple,
            R.drawable.mxy, R.drawable.mht, R.drawable.cup};

    interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bit_cat_p23_recy_view);

        listRv = findViewById(R.id.recyclerView);
        listRv.setLayoutManager(new LinearLayoutManager(this));
        listRv.setAdapter(new MyAdapterRC());

        onItemClickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(int position) {
                Toast.makeText(BitCatP23RecyViewActivity.this, "点击了: " + titles[position], Toast.LENGTH_SHORT).show();
            }
        };

    }

    class MyAdapterRC extends RecyclerView.Adapter<ViewHoldRCDto> {

        @NonNull
        @Override
        public ViewHoldRCDto onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(BitCatP23RecyViewActivity.this, R.layout.p22_item_list, null);
            return new ViewHoldRCDto(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHoldRCDto holder, int position) {
            holder.iconIv.setImageResource(icons[position]);
            holder.nameTv.setText(titles[position]);
            holder.introduceTv.setText(introduces[position]);

            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return titles.length;
        }


    }
}